package org.lsms.servlet;

/**
 * submit paper servlet
 * @author ZhanXueliang
 * 2010.8.18
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lsms.bean.Paper;
import org.lsms.dao.impl.DaoImpl;

import com.jspsmart.*;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class paper_submit extends HttpServlet {

	public void destroy() {
		super.destroy(); 
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
			//Initialization
			SmartUpload myupload = new SmartUpload();
			myupload.initialize(this.getServletConfig(), request, response);
			try{
				myupload.upload();
			}catch(SmartUploadException e){
				e.printStackTrace();
			}
			//check the file
			final String FileType = "doc,pdf";
			final int MaxSize = 1024*1024*50;
			com.jspsmart.upload.File file = myupload.getFiles().getFile(0);
			if(!file.isMissing()){
				String ext = file.getFileExt().toLowerCase();
				if(FileType.indexOf(ext)==-1){
					String info = "Paper format mismatch!Only .doc or .pdf files accept.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong format
					return;
				}
				int filesize = file.getSize();
				if(filesize>MaxSize){
					String info = "Paper too large size!Paper must be less than 50MB.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong size
					return;
				}	
			}else{
				String info = "Please select your paper to submit!";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);//no content
				return;
			}
			//upload
//			String filename = file.getFileName();
			String ext = file.getFileExt().toLowerCase();
//			java.util.Date date = new java.util.Date();
//			long time = date.getTime();
			String savename = "/paper/";
//			savename += String.valueOf(time); 
			DaoImpl impl = new DaoImpl();
			int max_id = impl.get_max_paper_id()+1;
			savename +=max_id;
//			savename +=filename;
			savename +=".";
			savename +=ext;
			try{
				file.saveAs(savename, file.SAVEAS_VIRTUAL);
			}catch(SmartUploadException e){
				e.printStackTrace();
			}
			
			Request req = myupload.getRequest();	
			HttpSession session = request.getSession();
			int userid = Integer.parseInt(session.getAttribute("userid").toString());
			
			Paper paper = new Paper();
			
			
			
			paper.setAuthorname(req.getParameter("authorname"));
			paper.setContactauthormail(req.getParameter("email"));
			paper.setContactauthortel(req.getParameter("tel"));
			
			paper.setAuthorid(userid);
			
			paper.setFirsttopic(req.getParameter("topic1"));
			paper.setSecondtopic(req.getParameter("topic2"));
			paper.setPapertitle(req.getParameter("title"));
			paper.setPaperabstract(req.getParameter("abstract"));
			paper.setPaperkeywords(req.getParameter("keywords"));
			paper.setSavename(savename);
			
//			DaoImpl impl = new DaoImpl();
			if(impl.submit_paper(paper)){
				String author_info = "Paper submit succeed!";
				response.sendRedirect("author/author_show_info.jsp?author_info="+author_info);
			}else{
				String author_info = "Paper submit failed!Please try again later.";
				response.sendRedirect("author/author_show_info.jsp?author_info="+author_info);
			}
	}

	public void init() throws ServletException {

	}

}
